var successUpdateCallback = function (data) {
    var tip = '';
    switch (data.status) {
        case 200:
            tip = '更新成功';
            window.setTimeout(function () {
                $('#requestCannel').click();
                window.location.reload();
            }, 1000);
            break;
    }
    tooltipWrong($('#requestConfuse'), tip, 2);
};
var errorUpdateCallback = function (data) {
    if (typeof (data.responseJSON) == "undefined") {
        tooltipWrong($('#requestConfuse'), '系统内部错误', 3);
    } else {
        tip = data.responseJSON.info;
        tooltipWrong($('#requestConfuse'), tip, 3);
    }
};
var successLoadCityCallback = function (data) {
    var tip = '';
    switch (data.status) {
        case 200:
            //
            var html = '';
            for (i = 0; i < data.data.length; i++) {
                html += "<a href=\"javascript:void(0)\" data-cityid=" + data.data[i].id + "  data-cityname=" + data.data[i].name + " >" + data.data[i].name + "</a>";
            }
            $("#cityList").show();
            $("#cityList").html(html);
            break;
    }
}

var errorLoadCityCallback = function (data) {


}
var successLoadAreaCallback = function (data) {
    switch (data.status) {
        case 200:
            //
            var html = '';
            for (i = 0; i < data.data.length; i++) {
                html += "<a href=\"javascript:void(0)\" data-areaid=" + data.data[i].id + "  data-areaname=" + data.data[i].name + " >" + data.data[i].name + "</a>";
            }
            $("#areaListDiv").show();
            $("#areaList").html(html);
            break;
    }

}

var errorLoadAreaCallback = function (data) {
    if (typeof (data.responseJSON) == "undefined") {
        tooltipWrong($('#requestConfuse'), '系统内部错误', 3);
    } else {
        tip = data.responseJSON.info;
        tooltipWrong($('#requestConfuse'), tip, 3);
    }

}

var successHandleCallback = function (data) {
    var tip = '';
    switch (data.status) {
        case 200:
            tip = '更新成功';
            window.setTimeout(function () {
                $('#requestCannel').click();
                window.location.reload();
            }, 1000);
            break;
    }
    tooltipWrong($('#requestEdit'), tip, 2);

}

var errorHandleCallback = function (data) {
    if (typeof (data.responseJSON) == "undefined") {
        tooltipWrong($('#requestEdit'), '系统内部错误', 3);
    } else {
        tip = data.responseJSON.info;
        tooltipWrong($('#requestEdit'), tip, 3);
    }
}

var testSuccessHandleCallback = function (data) {
    var tip = '';
    switch (data.status) {
        case 200:
            tip = '更新成功';
            window.setTimeout(function () {
                $('#requestCannel').click();
                window.location.reload();
            }, 1000);
            break;
    }
    tooltipWrong($('#requestEditStatus_test'), tip, 2);
}

var testErrorHandleCallback = function (data) {
    if (typeof (data.responseJSON) == "undefined") {
        tooltipWrong($('#requestEditStatus_tests'), '系统内部错误', 3);
    } else {
        tip = data.responseJSON.info;
        tooltipWrong($('#requestEditStatus_test'), tip, 3);
    }
}


$(function () {
    $('.confuse').on('click', function (e) {
        e.preventDefault();
        var obj = JSON.parse($(this).attr("data-json"));
        var id = obj.id;
        $("#requestConfuse").attr('data-value', id);
    });
    $(".edit").on('click', function (e) {
        e.preventDefault();
        $("#requestEdit").show();
        $("#requestEditStatus_test").hide();
        var obj = JSON.parse(($(this).attr("data-json")));
        $("#requestEditHotel").val(obj.hotelname);
        $("#requestEditAccount").val(obj.mobile);
        $("#requestEditAccountAreacode").val(obj.areacode);
        $("#addHotelRequestUniversalTime").val(obj.universaltime_id);
        $("#addHotelRequestCurrency").val(obj.currency_id);
        $("#requestEditUserName").val(obj.username);
        $("#requestEdit").data('dealerId',obj.dealerlogin_id);
        $("#requestEdit").data('enddate',obj.enddate);
        $("#requestEdit").attr("data-value", obj.id);
        $("#requestEditCity").val(obj.cityname);
        $("#requestEditCityId").val(obj.city_id);
        $("#requestEditDistrict").val(obj.districtName);
        $("#requestEditDistrictId").val(obj.district_id);
        //清除数据
        /*$("#requestEditCity").val('');
        $("#requestEditCityId").val('');
        $("#requestEditDistrict").val('');
        $("#requestEditDistrictId").val('');*/
    });

    $(".test").on('click', function (e) {
        e.preventDefault();
        $("#requestEdit").hide();
        $("#requestEditStatus_test").show();
        var obj = JSON.parse(($(this).attr("data-json")));
        $("#requestEditHotel").val(obj.hotelname);
        $("#requestEditAccount").val(obj.mobile);
        $("#requestEditAccountAreacode").val(obj.areacode);
        $("#addHotelRequestUniversalTime").val(obj.universaltime_id);
        $("#addHotelRequestCurrency").val(obj.currency_id);
        $("#requestEditUserName").val(obj.username);
        $("#requestEditStatus_test").data('dealerId',obj.dealerlogin_id);
        $("#requestEditStatus_test").data('enddate',obj.enddate);
        $("#requestEditStatus_test").attr("data-value", obj.id);
        $("#requestEditCity").val(obj.cityname);
        $("#requestEditCityId").val(obj.city_id);
        $("#requestEditDistrict").val(obj.districtName);
        $("#requestEditDistrictId").val(obj.district_id);
        //清除数据
       /* $("#requestEditCity").val('');
        $("#requestEditCityId").val('');
        $("#requestEditDistrict").val('');
        $("#requestEditDistrictId").val('');*/
    });

    $('#editModel').on('show.bs.modal', function(e) {
        var trigger = $(e.relatedTarget);
        var json = trigger.data('json');
        var params = new Object();
        params.countryId = json.country_id;
        var province = $('#provinceList .province');
        var provinceHtml = '';
        province.html('');
        ajaxRequest('/Home/Dealer/getCountryProvince', params, 'post', function(data){
            var info = data.data;
            for(var i=0;i<info.length;i++){
                provinceHtml += '<a href="javascript:void(0);"  data-provinceId ="'+info[i].id+'">'+info[i].name+'</a>'
            }
            province.append(provinceHtml);
        }, null);

    });

    $("#requestConfuse").on('click', function (e) {
        var id = $(this).attr('data-value');
        e.preventDefault();
        if (id == '') {
            tooltipWrong($(this), '客栈申请记录不存在', 1);
            return false;
        }
        var params = new Object();
        params.id = id;
        ajaxRequest('/Home/Dealer/confuse', params, 'post', successUpdateCallback, errorUpdateCallback);
    });

    $("#requestEditCity").on('click', function (e) {
        $("#provinceList").show();
    });

    $("#provinceList").on('click','.province>a', function (e) {
        //加载城市站点数据
        var params = new Object();
        params.province = $(this).attr("data-provinceId");
        ajaxRequest('/Home/OutInterface/city', params, 'post', successLoadCityCallback, errorLoadCityCallback);
    });
    $("#cityList").delegate("a", "click", function (e) {
        e.preventDefault();
        $("#requestEditCity").val($(this).attr("data-cityname"));
        $("#requestEditCityId").val($(this).attr("data-cityid"));
        //清除城市区域数据
        $("#requestEditDistrict").val('');
        $("#requestEditDistrictId").val('');
    });

    $("#areaList").delegate("a", "click", function (e) {
        e.preventDefault();
        $("#requestEditDistrict").val($(this).attr("data-areaname"));
        $("#requestEditDistrictId").val($(this).attr("data-areaid"));
    });
    $("#requestEditDistrict").on('click', function (e) {
        var params = new Object();
        params.city = $("#requestEditCityId").val();
        if ($("#requestEditCityId").val() == '') {
            return;
        }
        ajaxRequest('/Home/OutInterface/area', params, 'post', successLoadAreaCallback, errorLoadAreaCallback);
    });
    $("#provinceList").find(".closeCity").on('click', function () {
        $("#provinceList").hide();
    });
    $("#areaListDiv").find(".closeDistrict").on('click', function () {
        $("#areaListDiv").hide();
    });


    var cityResult = true;
    var hotelResult = true;
    var accountResult = true;
    var districtResult = true;
    var usernameResult = true;
    $('#requestEditCityId').on('blur', function (e) {
        e.preventDefault();
        if ($(this).val() == '') {
            tooltipWrong($('#requestEditCity'), "请选择城市", 3);
            cityResult = false;
            return false;
        }
        cityResult = true;
    });
    $('#requestEditDistrictId').on('blur', function (e) {
        e.preventDefault();
        if ($(this).val() == '') {
            tooltipWrong($('#requestEditDistrict'), "请选择城市所在区域", 3);
            districtResult = false;
            return false;
        }
        districtResult = true;
    });
    $("#requestEditAccount").on('blur', function (e) {
        e.preventDefault();
        switch (validateData($(this).val(), /^\d*$/)) {
            case 0:
                accountResult = false;
                tooltipWrong($(this), "手机号码格式不正确", 3);
                break;
            case -1:
                accountResult = false;
                tooltipWrong($(this), "手机号码不能为空", 3);
                break;
            case 1:
                accountResult = true;
                break;
            default:
                tooltipWrong($(this), "数据验证出现问题", 3);
        }

    });
    $("#requestEditUserName").on('blur', function (e) {
        e.preventDefault();
        if ($(this).val() == '') {
            tooltipWrong($('#requestEditUserName'), "主账号名称不能为空", 3);
            usernameResult = false;
            return false;
        }
        usernameResult = true;
    });
    $("#requestEditHotel").on('blur', function (e) {
        e.preventDefault();
        if ($(this).val() == '') {
            tooltipWrong($('#requestEditHotel'), "客栈名称不能为空", 3);
            hotelResult = false;
            return false;
        }
        hotelResult = true;

    });
    //审核开通
    $("#requestEdit").on("click", function (e) {
        e.preventDefault();
        //检查参数
        $('#requestEditAccount').blur();
        $('#requestEditCityId').blur();
        $('#requestEditDistrictId').blur();
        $('#requestEditHotel').blur();
        $('#requestEditUserName').blur();
        if (cityResult && hotelResult && accountResult && districtResult && usernameResult) {
            var params = new Object();
            params.mobile = $("#requestEditAccount").val();
            params.areaCode = $("#requestEditAccountAreacode").val();
            params.universaltimeId = $("#addHotelRequestUniversalTime").val();
            params.currencyId = $("#addHotelRequestCurrency").val();
            params.city = $("#requestEditCityId").val();
            params.district = $("#requestEditDistrictId").val();
            params.hotel = $("#requestEditHotel").val();
            params.id = $(this).attr("data-value");
            params.dealerId = $(this).data("dealerId");
            params.enddate = $(this).data("enddate");
            params.username = $("#requestEditUserName").val();
            params.usage = $("#requestEdit").val();
            ajaxRequest('/Home/Dealer/passDealerHotel', params, 'post', successHandleCallback, errorHandleCallback);
        }
    });

     //审核测试
    $("#requestEditStatus_test").on("click", function (e) {
        e.preventDefault();
        //检查参数
        $('#requestEditAccount').blur();
        $('#requestEditCityId').blur();
        $('#requestEditDistrictId').blur();
        $('#requestEditHotel').blur();
        $('#requestEditUserName').blur();
        if (cityResult && hotelResult && accountResult && districtResult && usernameResult) {
            var params = new Object();
            params.mobile = $("#requestEditAccount").val();
            params.areaCode = $("#requestEditAccountAreacode").val();
            params.universaltimeId = $("#addHotelRequestUniversalTime").val();
            params.currencyId = $("#addHotelRequestCurrency").val();
            params.city = $("#requestEditCityId").val();
            params.district = $("#requestEditDistrictId").val();
            params.hotel = $("#requestEditHotel").val();
            params.id = $(this).attr("data-value");
            params.dealerId = $(this).data("dealerId");
            params.enddate = $(this).data("enddate");
            params.username = $("#requestEditUserName").val();
            params.usage = $("#requestEditStatus_test").val();
            ajaxRequest('/Home/Dealer/passDealerHotel', params, 'post', testSuccessHandleCallback, testErrorHandleCallback);
        }
    });
});
